package BookManagement.controller;

import BookManagement.domain.Page;
import BookManagement.domain.Spend;
import BookManagement.domain.User;
import BookManagement.service.SpendService;
import BookManagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/spend")
public class SpendController {

    @Autowired
    @Qualifier("spendService")
    private SpendService spendService;

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @RequestMapping("/browseRecordByUser")
    public ModelAndView browseRecordByUser(HttpSession hs,String pageNo){
        User user =userService.findUserByUsername( (String) hs.getAttribute("usercurrent"));
        if(user==null){
            return new ModelAndView("redirect:/account/toLogin");
        }
        ModelAndView mav=new ModelAndView();
        Page p=new Page(pageNo,10,spendService.findSpendCount(user.getUserName()));
        HashMap<String,Object>map=new HashMap<>();
        map.put("username",user.getUserName());
        map.put("start",p.getStart());
        map.put("size",p.getPageSize());
        List<Spend> userSpend = spendService.findUserSpend(map);
        hs.setAttribute("spendUser",user.getName());
        mav.addObject("spendList",userSpend);
        mav.addObject("page",p);
        mav.setViewName("/user/recordList");
        return mav;
    }
}
